package com.ideal.manage.esms.controller.project;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.project.ParameterInfo;
import com.ideal.manage.esms.bean.project.UserManage;
import com.ideal.manage.esms.bean.project.UserManageDTO;
import com.ideal.manage.esms.service.monitor.MonitorService;
import com.ideal.manage.esms.service.project.ProjectService;
import com.ideal.manage.esms.service.project.UserManageService;
import com.ideal.manage.esms.service.system.ParameterService;
import com.ideal.manage.esms.service.system.RoleMenuService;
import com.ideal.manage.esms.service.system.RoleService;
import com.ideal.manage.esms.util.ExcelUtil;
import com.ideal.manage.esms.util.FileOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 83730 on 2018/4/17.
 */
@Controller
@RequestMapping("project/userManage")
public class UserManageController {
    @Resource
    private ProjectService projectService;
    @Resource
    private RoleService roleService;
    @Resource
    private ParameterService parameterService;
    @Resource
    private RoleMenuService roleMenuService;

    @Resource
    private MonitorService monitorService;
    @Autowired
    UserManageService userManageService;


    @RequestMapping("user_list")
    public void userList(Model model) {
        System.out.println(1111);
        List<String> stateList = userManageService.findStateList();
        model.addAttribute("stateList", stateList);
    }

    @RequestMapping("user_data")
    @ResponseBody
    public PageDto userData(int pageNum, HttpServletRequest request) throws ParseException {

        Page<UserManageDTO> page = userManageService.findAllParameterInfos(pageNum,request);
        List<UserManageDTO> parameterInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameterInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    @RequestMapping("user_add")
    public void userAdd(Model model) {
        UserManage manage = new UserManage();
        System.out.println(1111);
    }

    @RequestMapping("user_export")
    public void userEdit(Model model, HttpServletRequest request, HttpServletResponse response) throws ParseException {

        Page<UserManageDTO> page = userManageService.findAllParameterInfos(0,request);
        List<UserManageDTO> parameterInfos = page.getContent();

        ExcelUtil excelUtil = new ExcelUtil(UserManageDTO.class);
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.addHeader("Content-Disposition","attachment; filename="+ "user_data"+ LocalDate.now()+ ".xls");
        try {
            excelUtil.exportExcel("用户数据导出", response.getOutputStream(), parameterInfos);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("show_user_detail")
    public void showUserDetail(Model model, String id) {
        Map<String, Object> userDetailList = userManageService.findUserDetailList(id);
        model.addAttribute("model",userDetailList);
    }

    @RequestMapping("user_del")
    @ResponseBody
    public Result userDelete(Model model, String id) {
        System.out.println(1111);
        Result result = new Result();
        userManageService.deleteById(id);
        //result.setType("alert");
        //result.setMessage("操作成功!");
        return result;
    }


    @RequestMapping("deploy_edit_data")
    @ResponseBody
    public JSONArray deployEditData(Long id,Model model) {
        JSONArray list = new JSONArray();
        JSONObject jsonObject;
        for (int i = 1; i < 5; i++) {
            jsonObject = new JSONObject();
            // 假装一下null
            jsonObject.put("id", i);
            jsonObject.put("name", "比亚迪");
            jsonObject.put("pid", "type");
            list.add(jsonObject);
        }
        return list;
    }
}
